<?php
declare(strict_types=1);

namespace App\Service;

use App\JsonRpc\CalculatorServiceInterface;
use Hyperf\CircuitBreaker\Annotation\CircuitBreaker;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\ResponseInterface;

class BaseService
{
    /**
     * @Inject()
     * @var ResponseInterface
     */
    protected $response;

    /**
     * @Inject()
     * @var CalculatorServiceInterface
     */
    protected $calculatorService;

    /**
     * Notes:Rpc方法调用(服务提供方sleep2s，加入熔断处理)
     *
     * @CircuitBreaker(failCounter=1,timeout=0.1,fallback="App\Service\BaseService::searchFallback")
     */
    public function getRpcMicro(int $a, int $b)
    {
        return $this->calculatorService->add($a, $b);
    }

    public function searchFallback()
    {
        return $this->response->json([
            'code' => 400,
            'msg' => '服务熔断'
        ]);
    }
}